/**
 * 
 */
package com.sdu.passager.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.util.CollectionUtils;

import com.sdu.passager.bean.Field;
import com.sdu.passager.bean.TableBean;

/**
 * @author Administrator 将数据库中的表结构进行组装成TableBean
 */
public class TableUtil {
	
	private DriverManagerDataSource ds;
	private String genTables;/////要处理的表列表
	private String queryStr; ////查询语句
	private String seqName;////oracle seqName
	private Map<String,String> specialTypeMap;////特殊的处理类型,多个已,分割
	private static String COLUMNTN = "NUMBER";
	private static String ID = "id";



	//
	/**
	 * 获得组装好的TableBeans
	 * 
	 * @return
	 * @throws SQLException 
	 */
	public List<TableBean> getTableBeans(){
		////
		List<TableBean> tableBeans = new ArrayList<TableBean>();
		
		/////组装查询的sql语句list
		String[] genTablesArr = genTables.split(",");
		List<String> queryTableStr = new ArrayList<String>();
		for(String tableName : genTablesArr)
			queryTableStr.add(queryStr + tableName);
		
		if(CollectionUtils.isEmpty(queryTableStr))
			return new ArrayList<TableBean>();
		
	    ////查询 ,进行组装
		Connection conn = null;
		try {
			conn = ds.getConnection();
		} catch (SQLException e) {
			System.err.println("-----------连接失败----------------");
			e.printStackTrace();
		}
		Statement stmt = null;
		ResultSet rs = null;
		ResultSetMetaData meta = null;
		
		try {
			stmt = conn.createStatement();
			for(String _queryStr : queryTableStr){
				rs = stmt.executeQuery(_queryStr);
				meta = rs.getMetaData(); 
				
				String tableName = _queryStr.substring(queryStr.length()).trim();////获取,meta获取sqlserver获取为空
				int columnCount = meta.getColumnCount();////该表中的列数量
				List<Field> fields = new ArrayList<Field>();
				for(int i=1 ;i<=columnCount;i++){

					Field field = new Field();
					field.setName(meta.getColumnName(i).toLowerCase());////列名称 都改成小写模式
					if(ID.equals(field.getName())) continue;
					field.setType(meta.getColumnType(i));///类型
					
					String classNameValue = this.getSpecialTypeMap().get(meta.getColumnClassName(i));
					field.setClassName(classNameValue == null? meta.getColumnClassName(i) : classNameValue);////列在java中的类型
					field.setDisplaySize(meta.getColumnDisplaySize(i));///大小
					field.setTypeName(meta.getColumnTypeName(i));///类型名称
					field.setIsNull(meta.isNullable(i));////0-非空 1-可为空
					field.setAutoIncrement(meta.isAutoIncrement(i));///是否自动增长
					
					///对于oracle进行处理下
					String columnTN = meta.getColumnTypeName(i);///数据库展示名字
					int precision = meta.getPrecision(i);///数据库长度
					int scale = meta.getScale(i);////进度
					if(COLUMNTN .equals(columnTN)){
						if(scale > 0)
							field.setClassName("Double");
						else{
							if(precision >= 10)
								field.setClassName("Long");
							else
								field.setClassName("Integer");
						}
					}
					
					fields.add(field);
					
					//System.out.println(columnTN);
					//System.out.println(meta.getPrecision(i));
					//System.out.println(meta.getSchemaName(i));
					//System.out.println(meta.getScale(i));
				}
				
				TableBean tableBean = new TableBean();
				tableBean.setTableName(tableName);/////使用gen.conf中的配置的表名,可以是使用驼峰标识
				tableBean.setFields(fields);
				tableBean.setSeqName(seqName);
				tableBeans.add(tableBean);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println("------------获取表结构进行组装失败-------------------");
			e.printStackTrace();
		}finally{
			try {
				if(rs != null)  rs.close();
				if(stmt != null) stmt.close();
				if(conn != null) conn.close();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		
		return tableBeans;
	}
	
	

	public String getGenTables() {
		return genTables;
	}

	public void setGenTables(String genTables) {
		this.genTables = genTables;
	}

	public DriverManagerDataSource getDs() {
		return ds;
	}

	public void setDs(DriverManagerDataSource ds) {
		this.ds = ds;
	}



	public String getQueryStr() {
		return queryStr;
	}



	public void setQueryStr(String queryStr) {
		this.queryStr = queryStr;
	}



	public Map<String,String> getSpecialTypeMap() {
		return specialTypeMap;
	}



	public void setSpecialTypeMap(Map<String,String> specialTypeMap) {
		this.specialTypeMap = specialTypeMap;
	}



	public String getSeqName() {
		return seqName;
	}



	public void setSeqName(String seqName) {
		this.seqName = seqName;
	}



}
